#    This file is part of robo-ircbot.
#
#    robo-ircbot is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    robo-ircbot is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with robo-ircbot.  If not, see <http://www.gnu.org/licenses/>.

package App::Modular::Module::GenPass;
use strict;
use warnings;
use base qw(App::Modular::Module);

print ":: GenPass module loaded.\n";

# Events we want to receive.
sub events { return "PRIVMSG"; }

sub help {
        return (
                "Generate random passwords.",
                "gen <length>",
                "Options:",
                "  /all   Use the entire visible ASCII range.",
                "  /pub   Broadcast to the channel."
        );
}

# PRIVMSG events.
sub event_privmsg
{
        shift;
        my $e = shift;

        my $rval;
        my $out = "";
        my $i;

        my $help = $e->robo->mod("Helper") || return;

        # Admin commands for genpass module.
        if ($help->trigger_switch("gen", $e)) {
                return;
        }

        # gen trigger.
        if (my $h = $help->trig("gen", $e, 1)) {
                if ($h->{data} <= 0 || $h->{data} > 32) {
                        $e->s("PRIVMSG ".$e->dest." :\002gen\002: ".
                              "use a value between 0 and 32 douchebag.");
                        return;
                }

                if (exists($h->{all})) {
                        for ($i = 0; $i < $h->{data}; $i++) {
                                $rval = int(rand(95)) + 33;
                                $out .= chr($rval);
                        }
                } else {
                        for ($i = 0; $i < $h->{data}; $i++) {
                                $rval = int(rand(3));

                                # lower case a-z
                                if ($rval == 0) {
                                        $rval = int(rand(26)) + 97;
                                        $out .= chr($rval);
                                # upper case a-z
                                } elsif ($rval == 1) {
                                        $rval = int(rand(26)) + 65;
                                        $out .= chr($rval);
                                # numeric
                                } else {
                                        $rval = int(rand(10));
                                        $out .= $rval;
                                }
                        }
                }

                if (exists($h->{pub})) {
                        $e->s("PRIVMSG ".$e->dest." :\002gen\002: ".$out);
                } else {
                        $e->s("NOTICE ".$help->parse_nick($e->from).
                              " :\002gen\002: ".$out);
                }
        }
}

1;
